
//  不要在 onLaunch 的时候调用 getCurrentPage()此时 page 还没有生成。 
// getCurrentPage是获取当前页面的实例对象。
var app = getApp();

var util = require('../../../utils/util.js');
var today = util.formatDate(new Date());
Page({

  data: {

    birthdate: today,
    picture1: "../../../image/add.png",

    sex:"M"
  
  },


  bindDateChange: function (e) {
    this.setData({
      birthdate: e.detail.value
    });


  },

  radioChange:function(e){
    this.setData({
      sex: e.detail.value
    });
  },

  chooseImageTap: function () {
    let _this = this;
    wx.showActionSheet({
      itemList: ['从相册中选择', '拍照'],
      itemColor: "#f7982a",
      success: function (res) {
        if (!res.cancel) {
          if (res.tapIndex == 0) {
            _this.chooseWxImage('album')
          } else if (res.tapIndex == 1) {
            _this.chooseWxImage('camera')
          }
        }
      }
    })

  },
  chooseWxImage: function (type) {
    let _this = this;
    wx.chooseImage({
      sizeType: ['original', 'compressed'],
      sourceType: [type],
      success: function (res) {
        console.log(res);
        _this.setData({
          picture1: res.tempFilePaths[0],
        })
      }
    })
  },

  formSubmit: function (e) {
    var that = this;

    if (e.detail.value.name.length == 0) {
      util.showRejectToast('姓名不能为空');
      return;
    }

    if (e.detail.value.pid.length == 0) {
      util.showRejectToast('身份证不能为空');
      return;

    } else {

      var isIDCard2 = /^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}([0-9]|X)$/;
      var isIDCard1 = /^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$/;

      var result1 = isIDCard1.exec(e.detail.value.pid);
      var result2 = isIDCard2.exec(e.detail.value.pid);


      if (result1 == null && result2 == null) {
        util.showRejectToast('不是合法的身份证号码');
        return;
      }

    }

    var phone = /^1[0-9]{10}$/;
    var resultphone = phone.exec(e.detail.value.telephone);
    if (resultphone == null) {

      util.showRejectToast('不是合法的手机号码!');
    

      return;

    }


    var atkey = wx.getStorageSync("AccessToken");

    var formdata = {
      AccessToken: atkey, SUFFERERNAME: e.detail.value.name, PID: e.detail.value.pid, PHONE: e.detail.value.telephone, BIRTHDATE: that.data.birthdate, 
      SICKHISTORY: e.detail.value.description,SEX:that.data.sex
    }

    util.upload_file(app.globalData.addSufferer, that.data.picture1, 'picture1', formdata,
      function (res) {
        util.showSuccessToast(1)
      },
      function () {
        util.showRejectToast('数据保存失败!');
        return;
      }
    )

  },

  
  /**
 * 生命周期函数--监听页面加载
 */
  onLoad: function (options) {

    

  },

  onShow: function (options) {

    app.userlogin();

  },

})
